package com.xlh.outside;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.wzm.param.ApiResultPo.AddUserReslult;
import com.wzm.param.ApiResultPo.LoginReslult;
import com.xlh.Utils.GsonManager;
import com.xlh.conf.CommonConf;
import com.xlh.interf.IUiUpdateHandler;
import com.xlh.lib.Core.FIntent;
import com.xlh.thread.TestTask;

/**
 * Created by mike on 2017-12-19.
 */

public class UserInfoActivity extends  BaseActivity implements IUiUpdateHandler {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.user_info);

        //Thread th = new Thread(new TestTask(this));
        //th.start();
        app = (WzApplication)getApplication();
        bindControl();
        ininControlEvent();

    }


    private void bindControl(){
        cEditUeseName = (EditText)findViewById(R.id.user_inf_edt_username);

        cEditPassword = (EditText)findViewById(R.id.user_inf_edt_pwd);
        cEditPhone    =  (EditText)findViewById(R.id.user_inf_edt_phone);
        cButback      = (Button)findViewById(R.id.user_inf_but_back);
        cbutReset      = (Button)findViewById(R.id.user_inf_but_edit);


        //初始化数据
        cEditUeseName.setText(app.getActiveUser());

        cEditPassword.setText(app.getActiveUserPwd());

        cEditPhone.setText(app.getActiveUserPhone());

        //固定的不能设置的控件
        cEditUeseName.setEnabled(false);//账号
        cEditPhone.setEnabled(false);
        setEnbleStatus(false);


}
    private void setEnbleStatus(boolean t){
        if(t){
            cEditPassword.setEnabled(true);

        }else{

            cEditPassword.setEnabled(false);


        }

    }


    @Override
    public Handler getHandler() {
        return handler;
    }

    /**
     * 初始化事件
     */
    private void ininControlEvent(){


        handler = new Handler(){

            @Override
            public void handleMessage(Message msg) {

                String res  = msg.getData().getCharSequence("result").toString();
                LoginReslult data = GsonManager.getIns().jsonToAnyObject(res,LoginReslult.class);
                cbutReset.setEnabled(true);
                cbutReset.setText("修改");

                if(data.getCode()==1){
                    app.setActiveUserPwd(data.getData().getPassword());

                    Toast.makeText(UserInfoActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();

                }else{
                    Toast.makeText(UserInfoActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
                }
                super.handleMessage(msg);
            }
        };



        cbutReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(isEditing()){//修改状态下


                    String userName,password,param;

                    userName = cEditUeseName.getText().toString();
                    password = cEditPassword.getText().toString();

                    param = String.format("username=%s&newpwd=%s&oldpwd=%s",userName, password,app.getActiveUserPwd());
                    cbutReset.setEnabled(false);
                    setEditing(false);
                    setEnbleStatus(false);
                    Thread th = new Thread(new TestTask()
                            .setContext(UserInfoActivity.this)
                            .setUrl(CommonConf.API_USER_UPDATE + param)
                    );

                    th.start();


                }else{
                    setEnbleStatus(true);
                    setEditing(true);
                    cbutReset.setText("保存");
                }




            }
        });


        //back Button
        cButback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FIntent fIntent = new FIntent(UserInfoActivity.this, SettingActivity.class);
                startActivity(fIntent);
                finish();
            }
        });




    }

    public boolean isEditing() {
        return editing;
    }

    public void setEditing(boolean editing) {
        this.editing = editing;
    }





    /**
     * 用户名控件
     */
    private EditText cEditUeseName;
    private EditText cEditNickName;
    private EditText cEditPassword;
    private EditText cEditSex;
    private EditText cEditPhone;
    private Button cbutReset;

    private Button cButback;

    private Handler handler;
    private WzApplication app;
    private boolean editing=false;



}
